define(function(require) {

    'use strict';

    var Backbone = require('backbone'),
        Base = require('components/pop/base'),
        template = require('text!components/pop/templates/reply.html'),
        WbEditor = require('components/wb_editor_v2/com'),
        utils = require('core/utils');

    return Base.extend({

        template: _.template(template),
        className: function() {
            return this.options.className;
        },

        events: {
            'click .js-close': 'hide',
            'click .js-btn-confirm': 'triggerCallback'
        },

        initialize: function(options) {
            var view = this;

            Base.prototype.initialize.call(view, options);
            view.data = options.data;
        },

        render: function() {
            var view = this;

            var data = view.data;
            view.$el.html(view.template(data));

            view.wbEditorEle = view.$el.find('.wb-sender__input');

            view.wbEditor = new WbEditor({
                config: {
                    modules: ['emotion', 'hyperlink', 'articles', 'feature', 'goods', 'homepage'],
                    data: data
                },
                el: view.wbEditorEle
            });

            return view;
        },

        positioning: function() {
            var view = this;

            var className = view.options.className;
            view.el.className = className;

            view.$el.show();
            if (className.indexOf('left') !== -1) {
                view.$el.position({
                    of: view.target,
                    my: 'right center',
                    at: 'left center',
                    // collision: 'none'
                });
            } else {
                view.$el.position({
                    of: view.target,
                    my: 'left center',
                    at: 'right center',
                    // collision: 'none'
                });
            }
            view.wbEditor.focus();
        },

        setData: function(data) {
            var view = this;

            view.wbEditor.updateContent(data);
        },

        reset: function(obj) {
            var callback = obj.callback,
                target = obj.target,
                trigger = obj.trigger,
                data = obj.data,
                className = obj.className;

            this.options.className = className;
            this.setCallback(callback);
            this.setTarget(target);
            this.setTrigger(trigger);
            this.setData(data);
            this.positioning();
            this.show();
        },

        triggerCallback: function() {
            var view = this;

            var obj = view.wbEditor.getContent();
            if (!obj.content) {
                view.wbEditor.focus();
                return false;
            }

            view.callback.call(view, obj);
            view.hide();
            view.wbEditor.clear();
        }
    });
});
